package com.rqian.spring.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.rqian.spring.profile.exception.MD5Exception;

public class MD5Util {

	public static String hashStrWithMD5(String str) {
		StringBuffer sb = new StringBuffer();
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());

			byte byteData[] = md.digest();
			for (int i = 0; i < byteData.length; i++) {
				sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
			}
		} catch (NoSuchAlgorithmException e) {
			throw new MD5Exception();
		}

		return sb.toString();
	}
}
